<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2021/5/31
  Time: 15:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/common/common.jsp" %>
<html>
<head>
    <title>Title</title>
    <script>
        $(function () {
            $('#table').bootstrapTable({
                url: '<%=path%>/user/getInfo',
                method: 'post',                      //请求方式（*）
                contentType: "application/x-www-form-urlencoded",
                //toolbar: '#for_div',              //工具按钮用哪个容器
                striped: true,                     //是否显示行间隔色
                cache: true, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                uniqueId: "uid",                     //每一行的唯一标识，一般为主键列
                showToggle: true,                   //是否显示详细视图和列表视图的切换按钮
                // cardView: true,                    //是否显示详细视图
                detailView: true,                  //是否显示父子表
                //列
                columns: [{
                    checkbox: true,
                    visible: true                  //是否显示复选框
                }, {
                    field: 'uid',
                    title: 'ID'
                }, {
                    field: 'uname',
                    title: '用户名字'
                }, {
                    field: 'uid',
                    title: "操作",
                    //value daibiaozhi row 代表 数据 index 代表当前下表
                    formatter: function (value, row, index) {
                        var id = row.uid;
                        var result = "";
                        result += "<a href='javascript:toUpdate(" + id + ")' class='btn btn-xs blue' title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
                        result += "<a href='javascript:deleteInfoById(" + id + ")' class='btn btn-xs red' title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
                        return result;
                    }

                },]

            });

        })


        function deleteInfoById(id) {
            alert(id)
            if (confirm("确认删除吗")) {

                $.ajax({
                    url: "<%=path%>/user/deleteBy",
                    data: {id: id},
                    dataType: "json",
                    type: "post",
                    success: function (result) {
                        alert("删除成功");
                        $("#table").bootstrapTable("refresh");

                    },
                    error: function (result) {
                        console.log(result);
                        alert("网络异常请刷新");

                    }


                })
            }
        };

        function toUpdate (id) {
            BootstrapDialog.show({
                title: '修改',
                message: $('<div></div>').load('<%=path%>/user/huixuan?uid='+ id),
                closeable: true,
                buttons: [{
                    label: '取消',
                    action: function (dialog) {
                        dialog.close();
                    }

                }, {
                    label: '确定',
                    action: function (dialog) {
                        $.ajax({
                            url: "<%=path%>/user/updateInfo",
                            type: "post",
                            dataType: "json",
                            data: $("#update_form").serialize(),
                            success: function (data) {
                                toastr.success("修改成功")
                                $("#table").bootstrapTable("refresh");

                                dialog.close();

                            },
                            error: function (data) {
                                console.log(data);
                                alert("网络异常");
                            }

                        })

                    }
                }
                ]

            })
        }

        function insertInfo() {
            BootstrapDialog.show({
                title: '增加',
                message: $('<div></div>').load('<%=path%>/user/addInfo'),
                closeable: true,
                buttons: [{
                    label: '取消',
                    action: function (dialog) {
                        dialog.close();
                    }

                }, {
                    label: '确定',
                    action: function (dialog) {
                        alert($("#add_form").serialize())
                        $.ajax({
                            url: "<%=path%>/user/insertinto",
                            type: "post",
                            dataType: "json",
                            data: $("#add_form").serialize(),

                            success: function (data) {
                                $("#table").bootstrapTable("refresh");
                                toastr.success("增加成功")
                                dialog.close();
                            },
                            error: function (data) {
                                console.log(data);
                                alert("网络异常");
                            }

                        })

                    }
                }
                ]

            })
        }


    </script>
</head>
<body>
<input type="button" value="增加" onclick="insertInfo()">
<table id="table"></table>
</body>
</html>
